import {
  startZeroReport, asyncTableDetailPage } from './zeroReport.js';


let zeroInfo,
  isGetInfo = true;

setTimeout(() => {
  window.onload();
}, 3000);

window.onload = () => {
  if (isGetInfo) {
    isGetInfo = false;
    // 获得零申报信息
    window.postMessage(
      {
        event: 'getZeroInfo'
      },
      '*'
    );
  }
};


// 监听返回的事件
window.addEventListener(
  'message',
  async function ({ data }) {
    // 只监听来自于content script的信息
    if (data.sender !== 'content-script') {
      return false;
    }
    if (data.event === 'getZeroInfoBack') {
      zeroInfo = data.data;
      if (zeroInfo.isOpenZeroReport && zeroInfo.currentZeroInfo) {
        // 开始零申报
        startZeroReport(zeroInfo);
      } else if (zeroInfo.isOpenZeroReport) {
        // 同步申报表
        asyncTableDetailPage(zeroInfo);
      }
    }
    // if (data.event === 'baoBiaoDetailBack') {
    //   baoBiaoDetailBack(data.data);
    // }
    if (data.event === 'zeroReportEnd') {
      setTimeout(() => {
        window.postMessage(
          {
            event: 'asyncTableDetailRequest'
          },
          '*'
        );
      }, 1000);
    }
    if (data.event === 'asyncTableDetailRequestEnd') {
      window.postMessage(
        {
          event: 'clearZeroReport'
        },
        '*'
      );
      window.doLogOut.logOut();
    }
  },
  false
);
